#include "m_buildmanage.h"
#include "ui_m_buildmanage.h"
#include "m_buildinsert.h"
#include "m_roommanage.h"

QString global_id3;

M_buildManage::M_buildManage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::M_buildManage)
{
    ui->setupUi(this);
    querytable();
}

M_buildManage::~M_buildManage()
{
    delete ui;
}
void M_buildManage::querytable(){
    QString str=QString("select * from building");
    qDebug()<<str;
    model.setQuery(str);
    ui->tableView->setModel(&model);
    //ui->tableView->setColumnHidden(7,true);
    model.setHeaderData(0,Qt::Horizontal,"楼号");
    model.setHeaderData(1,Qt::Horizontal,"最大容量");
    model.setHeaderData(2,Qt::Horizontal,"现居住人数");
    model.setHeaderData(3,Qt::Horizontal,"楼栋居住学生性别");

}
void M_buildManage::on_BinsertBtn_clicked()
{
    M_buildInsert window;
    window.exec();
    querytable();
}

void M_buildManage::on_BdeleteBtn_clicked()
{
    int row = ui-> tableView ->currentIndex().row();
    QString id=model.data(model.index(row,0)).toString();

    QSqlQuery query;
    QString str=QString("delete from building where bui_bid='%1'").arg(id);
    if(query.exec(str)){
        QMessageBox::information(NULL,"tips","删除成功");
        querytable();
    }
    else {
        QMessageBox::warning(NULL,"Error","删除失败,必须先合理安排楼栋内学生才可删除楼栋");
    }
    querytable();
}

void M_buildManage::on_roommanage_clicked()
{
    int row = ui-> tableView ->currentIndex().row();
    QString id=model.data(model.index(row,0)).toString();

    global_id3=id;
    m_roomManage window;
    window.exec();
    querytable();
}

